package rssApp.controllers 
{
	import com.bit101.components.CheckBox;
	import com.bit101.components.Component;
	import common.events.PropertyEvent;
	import common.IComponentBuilder;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import rss.events.RssFeedEvent;
	import rss.IRssEntryModel;
	import rss.IRssFeedModel;
	import rss.IRssService;
	import rssApp.enums.RssAppEventEnum;
	import rssApp.events.RssAppCategoryEvent;
	import rssApp.events.RssAppFeedEvent;
	import rssApp.events.RssAppLinkEvent;
	import rssApp.IRssAppController;
	import rssApp.IRssAppModel;
	import rssApp.IRssAppViewMediator;
	import rssApp.mediators.RssAppViewMediator;
	
	
	/// @eventType rss.events.RssFeedEvent.LOAD
	[Event(name = "load", type = "rss.events.RssFeedEvent")] 
	
	
	/**
	 * TODO: document
	 * 
	 * @author Luke Van In
	 */
	public class RssAppController implements IRssAppController
	{
		
		//
		private var _eventDispatcher:IEventDispatcher;
		
		private var _service:IRssService;
		
		private var _model:IRssAppModel;
		
		
		
		/**
		 * 
		 * @param	service
		 * @param	viewMediator
		 */
		public function RssAppController(eventDispatcher:IEventDispatcher, service:IRssService, model:IRssAppModel) 
		{
			this._eventDispatcher = eventDispatcher;
			
			this._service = service;
			
			this._model = model;
			
			
			this.addEvents();
		}
		
		
		
		/**
		 * 
		 */
		private function addEvents():void
		{
			this._eventDispatcher.addEventListener(RssAppEventEnum.LOAD_FEED, this.loadFeedHandler);
			
			this._eventDispatcher.addEventListener(RssAppEventEnum.SELECT_CATEGORY, this.selectCategoryHandler);
			
			this._eventDispatcher.addEventListener(RssAppEventEnum.UNSELECT_CATEGORY, this.unselectCategoryHandler);
			
			this._eventDispatcher.addEventListener(RssFeedEvent.LOADED, this.feedLoadedHandler);
			
			this._eventDispatcher.addEventListener(RssAppEventEnum.LINK, this.linkHandler);
		}
		
		
		
		/**
		 * 
		 */
		private function removeEvents():void
		{
			this._eventDispatcher.removeEventListener(RssAppEventEnum.LOAD_FEED, this.loadFeedHandler);
			
			this._eventDispatcher.removeEventListener(RssAppEventEnum.SELECT_CATEGORY, this.selectCategoryHandler);
			
			this._eventDispatcher.removeEventListener(RssAppEventEnum.UNSELECT_CATEGORY, this.unselectCategoryHandler);
			
			this._eventDispatcher.removeEventListener(RssFeedEvent.LOADED, this.feedLoadedHandler);
			
			this._eventDispatcher.removeEventListener(RssAppEventEnum.LINK, this.linkHandler);
		}
		
		
		
		/**
		 * 
		 * @param	e
		 */
		private function loadFeedHandler(e:RssAppFeedEvent):void
		{
			var url:String = e.url;
			
			trace("RssAppController > loadFeedHandler: " + url);
			
			this._model.url = url;
			
			this._service.loadFeed(url);
		}
		
		
		
		/**
		 * 
		 * @param	e
		 */
		private function selectCategoryHandler(e:RssAppCategoryEvent):void
		{
			this.setCategory(e.category, true);
		}
		
		
		
		/**
		 * 
		 * @param	e
		 */
		private function unselectCategoryHandler(e:RssAppCategoryEvent):void
		{
			this.setCategory(e.category, false);
		}
		
		
		
		/**
		 * 
		 * @param	category
		 * @param	isSelected
		 */
		private function setCategory(category:String, isActive:Boolean):void
		{
			var categories:Dictionary = this._model.categories;
			
			categories[category] = isActive;
			
			this._model.categories = categories;
		}
		
		
		
		/**
		 * 
		 * @param	e
		 */
		private function feedLoadedHandler(e:RssFeedEvent):void
		{
			this._model.categories = this.getCategories(e.feed.entries);
			
			this._model.feed = e.feed;
		}
		
		
		
		/**
		 * 
		 * @param	entries
		 * @return
		 */
		private function getCategories(entries:Vector.<IRssEntryModel>):Dictionary
		{
			var categories:Dictionary = new Dictionary();
			
			for (var i:int = 0; i < entries.length; i++)
			{
				var entry:IRssEntryModel = entries[i];

				categories[entry.category] = true;
			}
			
			return categories;
		}
		
		
		
		
		/**
		 * 
		 * @param	e
		 */
		private function linkHandler(e:RssAppLinkEvent):void
		{
			this.openUrl(e.url);
		}
		
		
		
		/**
		 * 
		 * @param	url
		 */
		private function openUrl(url:String):void
		{
			var request:URLRequest = new URLRequest(url);
			
			navigateToURL(request, "_blank");
		}
		
		
				
		
		
		/**
		 * 
		 */
		public function dispose():void
		{
			this.removeEvents();		
		}
		
	}

}